package com.yan.refreshlistcontainer.component;


import com.yan.refreshlistcontainer.ResourceTable;
import com.yan.refreshlistcontainer.listener.RefreshListener;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.*;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.multimodalinput.event.MmiPoint;
import ohos.multimodalinput.event.TouchEvent;

public class RefreshListContainer extends ComponentContainer implements Component.TouchEventListener {
    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00202, "RefreshListContainer");

    private RefreshListener mRefreshListener;
    private Context mContext;
    private DependentLayout mHeadLayout;
    private DependentLayout mFootLayout;
    private ListContainer mListContainer;
    private DependentLayout mRootLayout;
    private float mStartY, mEndY;
    private Text mHeadText, mFootText;
    private Image mHeadImage, mFootImage;
    private boolean isScrollTop = true;
    private boolean isPull = false;
    private boolean isUp = false;
    private AnimatorProperty pullAnimator, upAnimator;
    private boolean isLoading = false;
    private int mFootLayoutHeight = 0;
    private BaseItemProvider mBaseItemProvider;

    public RefreshListContainer(Context context) {
        super(context);
        this.mContext = context;
        HiLog.error(LABEL, "RefreshListContainer:%{public}s", "RefreshListContainer");
        init();
    }

    public RefreshListContainer(Context context, AttrSet attrSet) {
        super(context, attrSet);
        this.mContext = context;
        HiLog.error(LABEL, "RefreshListContainer:%{public}s", "RefreshListContainer2");
        init();
    }

    public RefreshListContainer(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        this.mContext = context;
        HiLog.error(LABEL, "RefreshListContainer:%{public}s", "RefreshListContainer3");
        init();
    }


    private void init() {
        mRootLayout = (DependentLayout) LayoutScatter.getInstance(mContext)
                .parse(ResourceTable.Layout_refresh_layout, null, true);

        HiLog.error(LABEL, "init,rootLayout:%{public}s", mRootLayout);
        mHeadLayout = (DependentLayout) mRootLayout.findComponentById(ResourceTable.Id_head_layout);
        mFootLayout = (DependentLayout) mRootLayout.findComponentById(ResourceTable.Id_foot_layout);
        mListContainer = (ListContainer) mRootLayout.findComponentById(ResourceTable.Id_list_container);

        mHeadImage = (Image) mRootLayout.findComponentById(ResourceTable.Id_head_image);
        mHeadText = (Text) mRootLayout.findComponentById(ResourceTable.Id_head_text);
        mFootImage = (Image) mRootLayout.findComponentById(ResourceTable.Id_foot_image);
        mFootText = (Text) mRootLayout.findComponentById(ResourceTable.Id_foot_text);

        addComponent(mRootLayout);
        mFootLayoutHeight = mFootLayout.getHeight();
        HiLog.error(LABEL, "mFootLayoutHeight:%{public}s", mFootLayoutHeight);
        mHeadLayout.setVisibility(HIDE);
        mFootLayout.setVisibility(HIDE);
        mRootLayout.setTouchEventListener(this::onTouchEvent);
        mListContainer.setScrolledListener(new ScrolledListener() {
            @Override
            public void onContentScrolled(Component component, int i, int i1, int i2, int i3) {
                //如果i1为0则滑动到顶部了
                if (i1 == 0) {
                    isScrollTop = true;
                } else {
                    isScrollTop = false;
                }
            }
        });
    }


    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        int action = touchEvent.getAction();
        MmiPoint point = touchEvent.getPointerPosition(touchEvent.getIndex());
        HiLog.error(LABEL, "action:%{public}s", action);
        switch (action) {
            case TouchEvent.PRIMARY_POINT_DOWN:
                HiLog.error(LABEL, "onTouchEvent:%{public}s", "PRIMARY_POINT_DOWN");
                mStartY = point.getY();
                break;
            case TouchEvent.PRIMARY_POINT_UP:
                HiLog.error(LABEL, "onTouchEvent:%{public}s", "PRIMARY_POINT_UP");
                HiLog.error(LABEL, "isPull:%{public}s", isPull);
                if (isPull) {
                    isPull=false;
                    mHeadLayout.setMarginTop(0);
                    pullLoadingStatus();
                    if (this.mRefreshListener != null) {
                        this.mRefreshListener.pullRefresh();
                    }
                    mListContainer.scrollTo(0);
                    mListContainer.setEnabled(false);
                     isLoading = true;
                }
                if (isUp) {
                    isUp=false;
                    mFootLayout.setMarginBottom(0);
                    HiLog.error(LABEL, "mFootLayoutHeight1111:%{public}s", mFootLayoutHeight);
                    mListContainer.setMarginBottom(mFootLayoutHeight);
                    this.mBaseItemProvider.notifyDataSetItemInserted(mBaseItemProvider.getCount() - 1);
                    upLoadingStatus();

                    if (this.mRefreshListener != null) {
                        this.mRefreshListener.upLoadingMore();
                    }
                    isLoading=true;
                    mListContainer.setEnabled(false);
                }
                break;
            case TouchEvent.POINT_MOVE:
                HiLog.error(LABEL, "onTouchEvent:%{public}s", "HOVER_POINTER_MOVE");
                mEndY = point.getY();
                HiLog.error(LABEL, "onTouchEvent,mEndY:%{public}s,mStartY:%{public}s", mEndY, mStartY);
                HiLog.error(LABEL, "mListContainer.canScroll(DRAG_UP):%{public}s", mListContainer.canScroll(DRAG_UP));

                HiLog.error(LABEL, "isSocllTop:%{public}s", isScrollTop);
                if (!isLoading) {
                    if (isScrollTop && !isUp) {
                        if (mEndY - mStartY > 50) {
                            isPull = true;
                            if (pullAnimator != null) {
                                pullAnimator.stop();
                                mHeadImage.setRotation(0);
                            }
                            mHeadLayout.setVisibility(VISIBLE);
                            mHeadLayout.setMarginTop((int) (mEndY - mStartY) - 50);
                            if (mEndY - mStartY > 50 && mEndY - mStartY < 150) {
                                pullDefault();
                            } else {
                                pullStatus();
                            }
                            mListContainer.setEnabled(false);
                        }
                    }
                    if (!mListContainer.canScroll(DRAG_UP) && !isPull) {
                        if (mStartY - mEndY > 50) {
                            isUp = true;
                            if (upAnimator != null) {
                                upAnimator.stop();
                                mFootImage.setRotation(0);
                            }


                            HiLog.error(LABEL, "mStartY - mEndY:%{public}s", mStartY - mEndY);
                            mFootLayout.setVisibility(VISIBLE);
                            int des = (int) (mStartY - mEndY) - 50;
                            mListContainer.setMarginBottom(mFootLayoutHeight + des);
                            mFootLayout.setMarginBottom(des);
                            HiLog.error(LABEL, "mFootLayoutHeight:%{public}s", mFootLayoutHeight);
                            HiLog.error(LABEL, "mBaseItemProvider.getCount():%{public}s", mBaseItemProvider.getCount());
                            this.mBaseItemProvider.notifyDataSetItemInserted(mBaseItemProvider.getCount() - 1);

                            mListContainer.setEnabled(true);
                            if (mStartY - mEndY > 50 && mStartY - mEndY < 150) {
                                upDefault();
                            } else {
                                upStatus();
                            }

                        }
                    }
                }

                break;
            default:
        }
        return true;
    }


    private void pullDefault() {
        mHeadImage.setRotation(0);
        mHeadImage.setPixelMap(ResourceTable.Media_down_image);

        mHeadText.setText("下拉刷新");
    }

    private void pullStatus() {

        mHeadImage.setRotation(180);
        mHeadText.setText("松开立即刷新");
    }

    private void pullLoadingStatus() {
        mHeadText.setText("加载中...");
        mHeadImage.setPixelMap(ResourceTable.Media_image_loading);
        mHeadImage.setRotation(0);
        pullAnimator = mHeadImage.createAnimatorProperty();
        pullAnimator.rotate(360);
        pullAnimator.setDuration(1000);
        pullAnimator.setLoopedCount(Animator.INFINITE);
        pullAnimator.start();
    }

    private void upDefault() {
        mFootText.setText("上拉加载更多");
        mFootImage.setRotation(0);
        mFootImage.setPixelMap(ResourceTable.Media_up_image);
    }

    private void upStatus() {
        mFootImage.setRotation(180);
        mFootText.setText("松开立即加载");
    }

    private void upLoadingStatus() {
        mFootText.setText("加载中...");
        mFootImage.setPixelMap(ResourceTable.Media_image_loading);
        mFootImage.setRotation(0);
        upAnimator = mFootImage.createAnimatorProperty();
        upAnimator.rotate(360);
        upAnimator.setDuration(1000);
        upAnimator.setLoopedCount(Animator.INFINITE);
        upAnimator.start();
    }

    public void completeLoading() {
        if (pullAnimator != null) {
            pullAnimator.stop();

        }
        mHeadLayout.setVisibility(HIDE);
        pullDefault();

        if (upAnimator != null) {
            upAnimator.stop();
        }
        mFootLayout.setVisibility(HIDE);
        mListContainer.setMarginBottom(0);
        upDefault();
        if (isLoading){
            isLoading=false;
            mListContainer.setEnabled(true);
        }
      /*  if (isLoading){
            isLoading=false;
           mListContainer.scrollTo(mBaseItemProvider.getCount());
        }*/

    }


    public void setItemProvider(BaseItemProvider baseItemProvider) {
        this.mBaseItemProvider = baseItemProvider;
        mListContainer.setItemProvider(this.mBaseItemProvider);

    }

    public void setRefreshListener(RefreshListener refreshListener) {
        this.mRefreshListener = refreshListener;
    }


}
